package org.example.src.controller;

import org.example.src.dto.request.FeedbackRequest;
import org.example.src.dto.response.ApiResponse;
import org.example.src.dto.response.FeedbackResponse;
import org.example.src.entity.Feedback;
import org.example.src.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/feedbacks")
@CrossOrigin(origins = "*")
public class FeedbackController extends BaseController {

    @Autowired
    private FeedbackService feedbackService;

    @GetMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ApiResponse<List<Feedback>>> getAllFeedbacks() {
        List<Feedback> feedbacks = feedbackService.findAllFeedbacks();
        return success(feedbacks);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'TEACHER', 'COMPANY')")
    public ResponseEntity<ApiResponse<Feedback>> getFeedbackById(@PathVariable Long id) {
        Feedback feedback = feedbackService.findFeedbackById(id);
        if (feedback == null) {
            return notFound("反馈未找到");
        }
        return success(feedback);
    }

    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'TEACHER', 'COMPANY')")
    public ResponseEntity<ApiResponse<Feedback>> createFeedback(@Valid @RequestBody FeedbackRequest request) {
        Feedback createdFeedback = feedbackService.createFeedback(request);
        return created(createdFeedback);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ResponseEntity<ApiResponse<Void>> deleteFeedback(@PathVariable Long id) {
        feedbackService.deleteFeedback(id);
        return noContent();
    }

    @GetMapping("/student/{studentId}")
    public ResponseEntity<ApiResponse<List<Feedback>>> getFeedbacksByStudentId(@PathVariable Long studentId) {
        List<Feedback> feedbacks = feedbackService.findFeedbacksByStudentId(studentId);
        return success(feedbacks);
    }

    @GetMapping("/teacher/{teacherId}")
    public ResponseEntity<ApiResponse<List<Feedback>>> getFeedbacksByTeacherId(@PathVariable Long teacherId) {
        List<Feedback> feedbacks = feedbackService.findFeedbacksByTeacherId(teacherId);
        return success(feedbacks);
    }

    @GetMapping("/company/{companyId}")
    public ResponseEntity<ApiResponse<List<Feedback>>> getFeedbacksByCompanyId(@PathVariable Long companyId) {
        List<Feedback> feedbacks = feedbackService.findFeedbacksByCompanyId(companyId);
        return success(feedbacks);
    }

    @GetMapping("/from/{userId}")
    public ResponseEntity<ApiResponse<List<Feedback>>> getFeedbacksByFromUserId(@PathVariable Long userId) {
        List<Feedback> feedbacks = feedbackService.findFeedbacksByFromUserId(userId);
        return success(feedbacks);
    }

    @GetMapping("/to/{userId}")
    public ResponseEntity<ApiResponse<List<Feedback>>> getFeedbacksByToUserId(@PathVariable Long userId) {
        List<Feedback> feedbacks = feedbackService.findFeedbacksByToUserId(userId);
        return success(feedbacks);
    }

    @GetMapping("/to/{userId}/responses")
    public ResponseEntity<ApiResponse<List<FeedbackResponse>>> getFeedbackResponsesByToUserId(@PathVariable Long userId) {
        List<FeedbackResponse> feedbacks = feedbackService.getFeedbacksByToUserId(userId);
        return success(feedbacks);
    }
}
